#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "cTools.h"
#include <stdlib.h>
#include "data.h"
#include <Tlhelp32.h>
#include <direct.h>
#pragma comment(lib, "Winmm.lib")

BOOL WINAPI DllMain( HMODULE hModule, DWORD  dwCallType, LPVOID lpReserved );
DWORD WINAPI HookThread( LPVOID lpParam );
DWORD WINAPI BotThread( LPVOID lpParam );

extern cTimer Timer;
extern cTime Time;

bool sdblue, sdred, sddragon, sdnashor, sdwraiths, sdwolves, sdgolems;
bool activeblue, activered, activedragon, activenashor, activewraiths, activewolves, activegolems;
char* Dirs[] = {
	"Sounds\\Blue start.wav",
	"Sounds\\Blue in 1 minute.wav",
	"Sounds\\Blue in 15 seconds.wav",
	"Sounds\\Blue Spawned.wav",
	"Sounds\\Dragon start.wav",
	"Sounds\\Dragon in 1 minute.wav",
	"Sounds\\Dragon in 15 seconds.wav",
	"Sounds\\Dragon spawned.wav",
	"Sounds\\Nashor start.wav",
	"Sounds\\Nashor in 1 minute.wav",
	"Sounds\\Nashor in 15 seconds.wav",
	"Sounds\\Nashor spawned.wav",
	"Sounds\\Red start.wav",
	"Sounds\\Red in 1 minute.wav",
	"Sounds\\Red in 15 seconds.wav",
	"Sounds\\Red Spawned.wav",
	"Sounds\\Golems start.wav",
	"Sounds\\Golems in 15 seconds.wav",
	"Sounds\\Golems spawned.wav",
	"Sounds\\Wolves start.wav",
	"Sounds\\Wolves in 15 seconds.wav",
	"Sounds\\Wolves spawned.wav",
	"Sounds\\Wraiths start.wav",
	"Sounds\\Wraiths in 15 seconds.wav",
	"Sounds\\Wraiths spawned.wav"
};

DWORD WINAPI ThreadProc( Position pos)
{
	//char CurrentPath[_MAX_PATH];
	//getcwd(CurrentPath, _MAX_PATH);

	//MessageBoxA(0, CurrentPath, 0, 0);

	unsigned int timer = 0;
	unsigned int spawntime = 0;
	switch( pos ){
	case Blue: spawntime = 300; 
		Timer.BlueStart = 1;
		activeblue = 1;
		for(;;){
			if(sdblue){
				activeblue = 0;
				return 0;
			}
			Time.BlueTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if ( spawntime - timer == 60) {
				PlaySoundA(Dirs[BlueMinute],NULL,SND_FILENAME|SND_ASYNC);
				Timer.BlueMinute = 1;
			}
			else if( spawntime - timer == 15) {
				PlaySoundA(Dirs[BlueSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.BlueSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[BlueSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.BlueSpawned = 1;
				activeblue = 0;
				return 0;
			}
		}

		break;
	case Red: spawntime = 300; 
		Timer.RedStart = 1;
		activered = 1;
		for(;;){
			if(sdred){
				activered = 0;
				return 0;
			}
			Time.RedTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if ( spawntime - timer == 60) {
				PlaySoundA(Dirs[RedMinute],NULL,SND_FILENAME|SND_ASYNC);
				Timer.RedMinute = 1;
			}
			else if( spawntime - timer == 15) {
				PlaySoundA(Dirs[RedSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.RedSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[RedSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.RedSpawned = 1;
				activered = 0;
				return 0;
			}
		}

		break;
	case Dragon: spawntime = 360;
		Timer.DragonStart = 1;
		activedragon = 1;
		for(;;){
			if(sddragon){
				activedragon = 0;
				return 0;
			}
			Time.DragonTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if ( spawntime - timer == 60) {
				PlaySoundA(Dirs[DragonMinute],NULL,SND_FILENAME|SND_ASYNC);
				Timer.DragonMinute = 1;
			}
			else if( spawntime - timer == 15) {
				PlaySoundA(Dirs[DragonSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.DragonSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[DragonSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.DragonSpawned = 1;
				activedragon = 0;
				return 0;
			}
		}
		break;
	case Wraiths: spawntime = 50; 
		Timer.WraithsStart = 1;
		activewraiths = 1;
		for(;;){
			if(sdwraiths){
				activewraiths = 0;
				return 0;
			}
			Time.WraithsTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if( spawntime - timer == 15) {
				PlaySoundA(Dirs[WraithsSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.WraithsSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[WraithsSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.WraithsSpawned = 1;
				activewraiths = 0;
				return 0;
			}
		}
		break;
	case Wolves: spawntime = 60;
		Timer.WolvesStart = 1;
		activewolves = 1;
		for(;;){
			if(sdwolves){
				activewolves = 0;
				return 0;
			}
			Time.WolvesTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if( spawntime - timer == 15) {
				PlaySoundA(Dirs[WolvesSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.WolvesSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[WolvesSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.WolvesSpawned = 1;
				activewolves = 0;
				return 0;
			}
		}
		break;
	case Golems: spawntime = 60;
		Timer.GolemsStart = 1;
		activegolems = 1;
		for(;;){
			if(sdgolems){
				activegolems = 0;
				return 0;
			}
			Time.GolemsTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if( spawntime - timer == 15) {
				PlaySoundA(Dirs[GolemsSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.GolemsSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[GolemsSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.GolemsSpawned = 1;
				activegolems = 0;
				return 0;
			}
		}
		break;	
	case Nashor: spawntime = 420;
		Timer.NashorStart = 1;
		activenashor = 1;
		for(;;){
			if(sdnashor){
				activenashor = 0;
				return 0;
			}
			Time.NashorTime = spawntime - timer;
			Sleep(1000);
			timer++;
			if ( spawntime - timer == 60 ) {
				PlaySoundA(Dirs[NashorMinute],NULL,SND_FILENAME|SND_ASYNC);
				Timer.NashorMinute = 1;
			}
			else if( spawntime - timer == 15) {
				PlaySoundA(Dirs[NashorSoon],NULL,SND_FILENAME|SND_ASYNC);
				Timer.NashorSoon = 1;
			}
			else if( spawntime - timer == 0 ){
				PlaySoundA(Dirs[NashorSpawned],NULL,SND_FILENAME|SND_ASYNC);
				Timer.NashorSpawned = 1;
				activenashor = 0;
				return 0;
			}
		}
		break;
	
	}

}

DWORD WINAPI BotThread( LPVOID lpParam )
{
	static bool num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0;
	static HANDLE hnum1=0, hnum2=0, hnum3=0, hnum4=0, hnum5=0, hnum6=0, hnum7=0;

	for(;;Sleep(35)){
		if ( GetAsyncKeyState(VK_NUMPAD1) ){
			if(activeblue){
				sdblue = 1;
				Sleep(1000);
				sdblue = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Blue, NULL, NULL);
			PlaySoundA(Dirs[BlueStart],NULL,0/*SND_FILENAME|SND_ASYNC*/);
			while(GetAsyncKeyState(VK_NUMPAD1)); 
			
		}
		else if( GetAsyncKeyState(VK_NUMPAD2) ){
			if(activered){
				sdred = 1;
				Sleep(1000);
				sdred = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Red, NULL, NULL);
			PlaySoundA(Dirs[RedStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD2)); 
		}
		else if( GetAsyncKeyState(VK_NUMPAD3) ){
			if(activedragon){
				sddragon = 1;
				Sleep(1000);
				sddragon = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Dragon, NULL, NULL);
			PlaySoundA(Dirs[DragonStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD3)); 
		}
		else if( GetAsyncKeyState(VK_NUMPAD7) ){
			if(activewolves){
				sdwolves = 1;
				Sleep(1000);
				sdwolves = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Wolves, NULL, NULL);
			PlaySoundA(Dirs[WolvesStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD7)); 
		}
		else if( GetAsyncKeyState(VK_NUMPAD8) ){
			if(activewraiths){
				sdwraiths = 1;
				Sleep(1000);
				sdwraiths = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Wraiths, NULL, NULL);
			PlaySoundA(Dirs[WraithsStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD8)); 
		}
		else if( GetAsyncKeyState(VK_NUMPAD9) ){
			if(activegolems){
				sdgolems = 1;
				Sleep(1000);
				sdgolems = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Golems, NULL, NULL);
			PlaySoundA(Dirs[GolemsStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD9)); 
		}
		else if( GetAsyncKeyState(VK_NUMPAD5) ){
			if(activenashor){
				sdnashor = 1;
				Sleep(1000);
				sdnashor = 0;
			}
			CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)Nashor, NULL, NULL);
			PlaySoundA(Dirs[NashorStart],NULL,SND_FILENAME|SND_ASYNC);
			while(GetAsyncKeyState(VK_NUMPAD5)); 
		}
		else if ( GetAsyncKeyState(VK_F12) ){
			FreeLibrary(GetModuleHandle(0));
			return 0;
		}
		
	} 
}
BOOL WINAPI DllMain( HMODULE hModule, DWORD  dwCallType, LPVOID lpReserved )
{
	switch( dwCallType )
	{
		case DLL_PROCESS_ATTACH:
			CreateThread(NULL, NULL, HookThread, NULL, NULL, NULL);
			CreateThread(NULL, NULL, BotThread, NULL, NULL, NULL);
		break;

		case DLL_PROCESS_DETACH:
		break;
	}
	return true;
}